<?php
namespace App\Http\Controllers\Index;

use App\Http\Controllers\Controller;
use App\Logic\Code;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Logic\finger\UserBalanceLogic;
use Illuminate\Support\Facades\Validator;


class UserBalanceController extends Controller
{
    /**
     *  收益详情，收益记录,划转
     */


    /**
     * @OA\Post(
     *     path="/userBalanceXq",
     *     tags={"用户钱包"},
     *     summary="详情",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="token", type="string", description="币种类型"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="total总，already已提现，rel剩余",
     *     )
     * )
     */
    public function Xq(Request $request)
    {
        $post_data = $request->all();

        $validateRule = [
            'token' => 'required',
        ];
        $errorMsg = [
            'token.required' => '请传入币种信息',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }

        $power_device = DB::table('miner_user as mu')
            ->leftJoin('miner_info as mi','mi.mid','=','mu.mid')
            ->where('mu.user_name',$this->user['username'])
            ->where('mi.note',1)
            ->count();
        $power_capacity = DB::table('miner_user as mu')
            ->leftJoin('miner_host as mh','mh.mid','=','mu.mid')
            ->leftJoin('miner_info as mi','mi.mid','=','mu.mid')
            ->where('mi.note',1)
            ->where('mu.user_name',$this->user['username'])
            ->sum('mh.hspace');

        $miner_device = DB::table('miner_user as mu')
            ->leftJoin('miner_info as mi','mi.mid','=','mu.mid')
            ->where('mu.user_name',$this->user['username'])
            ->where('mi.note',2)
            ->count();
        $miner_capacity = DB::table('miner_user as mu')
            ->leftJoin('miner_host as mh','mh.mid','=','mu.mid')
            ->leftJoin('miner_info as mi','mi.mid','=','mu.mid')
            ->where('mi.note',2)
            ->where('mu.user_name',$this->user['username'])
            ->sum('mh.hspace');

        $date = date('Y-m-d');

        $yesterday = date('Y-m-d',strtotime($date)-85400);

        //昨日收益
        $yesterday_profit = DB::table('miner_balance as mb')
            ->leftJoin('miner_user as mu','mu.mid','=','mb.mid')
            ->where('mb.type','mining')
            ->where('mu.user_name',$this->user['username'])
            ->where('mb.timestamp','>',$yesterday)
            ->sum('amount');



        $today_profit = DB::table('miner_balance as mb')
            ->leftJoin('miner_user as mu','mu.mid','=','mb.mid')
            ->where('mb.type','mining')
            ->where('mu.user_name',$this->user['username'])
            ->where('mb.timestamp','>',$date)
            ->sum('amount');
        $yesterday_profit = $yesterday_profit-$today_profit;


        //总收益，已提现，剩余
        /*
        $total = DB::table('user_balance')
            ->where('user_name',$this->user['username'])
            ->where('token',$post_data['token'])
            ->whereIn('type',['profit'])
            ->sum('amount');
            */
         $total = DB::table('miner_balance as mb')
            ->leftJoin('miner_user as mu','mu.mid','=','mb.mid')
            ->where('mb.type','mining')
            ->where('mu.user_name',$this->user['username'])
            ->sum('amount');    
        $already = DB::table('user_balance')
            ->where('user_name',$this->user['username'])
            ->where('token',$post_data['token'])
            ->whereIn('type',['withdraw','transfer','reject'])
            ->sum('amount');
        $rel = $total + $already;
        $arr = [
            'total' => $total,   //总收益
            'already' => $already,  //已划转
            'rel' => $rel,          //剩余
            'power_device' => $power_device,  //算力设备数
            'power_capacity' => $power_capacity,  //算力容量数
            'miner_device' => $miner_device,        //矿机设备数
            'miner_capacity' => $miner_capacity,    //矿机容量数
            'yesterday_profit' =>$yesterday_profit,     //昨日收益
            'today_profit' =>$today_profit,     //昨日收益
            // 'fil' => $this->user['fil'],
            'fil' => number_format($this->user['fil'] , 6),//保留6位小数
        ];
        return s_msg('',$arr);
    }


    /**
     * @OA\Post(
     *     path="/admin/user_balance_profit",
     *     tags={"用户钱包"},
     *     summary="记录",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="key", type="string", description="键为 username,phone"),
     *                 @OA\Property(property="val", type="string", description="值"),
     *                 @OA\Property(property="type", type="string", description="类型搜索 只有fil"),
     *                 @OA\Property(property="level", type="number", description="用户等级搜索，暂无"),
     *                 @OA\Property(property="token", type="string", description="tokne搜索 FIL,ETH"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function profit(Request $request)
    {

        $post_data = $request->all();
        $where = [];
        if($request->has('limit')){
            $limit = $request->input('limit');
        }else{
            $limit = 15;
        }

        if(isset($post_data['key']) && isset($post_data['val']) ){

            if(!in_array($post_data['key'],['phone','username'])){
                return e_msg(123456,'只能搜索phone,username');
            }
            $key = 'u.'.$post_data['key'];
            $where[] = [$key,'=',$post_data['val']];
        }

        if(isset($post_data['level'])){

            if(!in_array($request->input('level'),[1,2,3,4,5,6])){
                return e_msg('level只能传入1，2，3，4，5,6');
            }
            $where[] = ['u.level','=',$request->input('level')];

        }

        if($request->has('token') && $request->input('token')!=''){
            $where[] = ['ub.token','=',$request->input('token')];
        }
        $miner_list = Db::table('user_balance as ub')
            ->leftJoin('user as u','ub.user_name','=','u.username')
            ->where($where)
            ->select('ub.*','u.phone','u.level')
            ->orderBy('ub.id','desc')
            ->paginate($limit);
        return s_msg('',$miner_list);

    }



    /**
     * @OA\Post(
     *     path="/user_balance_transfer",
     *     tags={"用户钱包"},
     *     summary="划转",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="mid", type="number", description="矿机id"),
     *                 @OA\Property(property="code", type="string", description="短信验证码"),
     *                 @OA\Property(property="amount", type="string", description="划转数量"),
     *                 @OA\Property(property="token", type="string", description="划转币种  ETH"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function transfer(Request $request)
    {
        $data = $request->all();
        $scene = $request->path();
        return UserBalanceLogic::transfer($data,$this->user,$scene);

    }


    /**
     * @OA\Post(
     *     path="/admin/user_balance_transfer",
     *     tags={"用户钱包"},
     *     summary="系统划转",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="mid", type="number", description="矿机id"),
     *                 @OA\Property(property="code", type="string", description="短信验证码"),
     *                 @OA\Property(property="amount", type="string", description="划转数量"),
     *                 @OA\Property(property="token", type="string", description="划转币种  ETH"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function sys_transfer(Request $request)
    {
        $data = $request->all();
        return UserBalanceLogic::sys_transfer($data);

    }


}
